import { h, ref } from 'vue';
import { cloneDeep } from 'lodash-es';
import { FormSchema } from '@/components/Form';
import { defShortcuts } from '@/utils/dateUtil';
import { renderOptionTag } from '@/utils';
import { useDictStore } from '@/store/modules/dict';

const dict = useDictStore();

export class State {
  public id = 0; // id
  public title = ''; // 活动名称
  public startAt = ''; // 开始时间
  public endAt = ''; // 结束时间
  public streaming = ''; // 直播流地址
  public rule = ''; // 规则
  public camera1 = ''; // 机位1
  public camera2 = ''; // 机位2
  public camera3 = ''; // 机位3
  public camera4 = ''; // 机位4
  public camera5 = ''; // 机位5
  public camera6 = ''; // 机位6
  public camera7 = ''; // 机位7
  public camera8 = ''; // 机位8
  public createAt = ''; // 添加时间
  public updateAt = ''; // 更新时间
  public status = 1; // 状态

  constructor(state?: Partial<State>) {
    if (state) {
      Object.assign(this, state);
    }
  }
}

export function newState(state: State | Record<string, any> | null): State {
  if (state !== null) {
    if (state instanceof State) {
      return cloneDeep(state);
    }
    return new State(state);
  }
  return new State();
}

// 表单验证规则
export const rules = {
  title: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入活动名称',
  },
  startAt: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入开始时间',
  },
  endAt: {
    required: true,
    trigger: ['blur', 'input'],
    type: 'string',
    message: '请输入结束时间',
  },
};

// 表格搜索表单
export const schemas = ref<FormSchema[]>([
  {
    field: 'title',
    component: 'NInput',
    label: '活动名称',
    componentProps: {
      placeholder: '请输入活动名称',
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'startAt',
    component: 'NDatePicker',
    label: '开始时间',
    componentProps: {
      type: 'datetime',
      clearable: true,
      shortcuts: defShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'endAt',
    component: 'NDatePicker',
    label: '结束时间',
    componentProps: {
      type: 'datetime',
      clearable: true,
      shortcuts: defShortcuts(),
      onUpdateValue: (e: any) => {
        console.log(e);
      },
    },
  },
  {
    field: 'status',
    component: 'NRadioGroup',
    label: '状态',
    giProps: {
      //span: 24,
    },
    componentProps: {
      options: dict.getOption('sys_finish'),
      onUpdateChecked: (e: any) => {
        console.log(e);
      },
    },
  },
]);

// 表格列
export const columns = [
  {
    title: 'id',
    key: 'id',
    align: 'left',
    width: -1,
  },
  {
    title: '活动名称',
    key: 'title',
    align: 'left',
    width: -1,
  },
  {
    title: '开始时间',
    key: 'startAt',
    align: 'left',
    width: -1,
  },
  {
    title: '结束时间',
    key: 'endAt',
    align: 'left',
    width: -1,
  },
  {
    title: '直播流地址',
    key: 'streaming',
    align: 'left',
    width: -1,
  },
  {
    title: '机位1',
    key: 'camera1',
    align: 'left',
    width: -1,
  },
  {
    title: '机位2',
    key: 'camera2',
    align: 'left',
    width: -1,
  },
  {
    title: '机位3',
    key: 'camera3',
    align: 'left',
    width: -1,
  },
  {
    title: '机位4',
    key: 'camera4',
    align: 'left',
    width: -1,
  },
  {
    title: '机位5',
    key: 'camera5',
    align: 'left',
    width: -1,
  },
  {
    title: '机位6',
    key: 'camera6',
    align: 'left',
    width: -1,
  },
  {
    title: '机位7',
    key: 'camera7',
    align: 'left',
    width: -1,
  },
  {
    title: '机位8',
    key: 'camera8',
    align: 'left',
    width: -1,
  },
  {
    title: '添加时间',
    key: 'createAt',
    align: 'left',
    width: -1,
  },
  {
    title: '更新时间',
    key: 'updateAt',
    align: 'left',
    width: -1,
  },
  {
    title: '状态',
    key: 'status',
    align: 'left',
    width: -1,
    render(row: State) {
      return renderOptionTag('sys_finish', row.status);
    },
  },
];

// 加载字典数据选项
export function loadOptions() {
  dict.loadOptions(['sys_finish']);
}